$(function(){
	//刷新浏览器是更新页面数据
	// if($('input[name=buytype]:checked').val()=='1'){
	// 	var str ='';
	// 	str += '<tr class="hdfk"><td width="720px;"></td><td width="120px;">货到付款手续费：</td><td width="100px;">￥<span>10.00</span></td></tr>';
	// 	$('tr.gspsum').after(str);
	// 	var rp = parseInt($('.ap').text().replace(',','')) + parseInt($('input[name=transporttype]:checked').attr('tp')) + 10;
	// 	$('.rp').html(rp + '.00');
	// }else{
	// 	var rp = parseInt($('.ap').text().replace(',','')) + parseInt($('input[name=transporttype]:checked').attr('tp'));
	// 	$('.rp').html(rp + '.00');
	// }
	// if($('input[name=transporttype]:checked').val()=='1'){
	// 	$('.tp').html('15.00');
	// }else{
	// 	$('.tp').html('20.00');
	// }

	// var payPrice = 0;
	// var transportPrice = 0;
	// //判断选择的支付方式
	// $('input[name=buytype]').click(function(){
	// 	if($(this).attr('value') == '1'){
	// 		payPrice += parseInt($(this).attr('pp'));
	// 		var str = '';
	// 		str += '<tr class="hdfk"><td width="720px;"></td><td width="120px;">货到付款手续费：</td><td width="100px;">￥<span>10.00</span></td></tr>';
	// 		$('tr.gspsum').after(str);
	// 		var rp = parseInt($('.ap').text().replace(',','')) + parseInt($('input[name=transporttype]:checked').attr('tp')) + payPrice;
	// 		$('.rp').html(rp + '.00');
	// 	}else{
	// 		payPrice = 0;
	// 		$('tr.hdfk').remove();
	// 		var rp = parseInt($('.ap').text().replace(',','')) + parseInt($('input[name=transporttype]:checked').attr('tp')) + payPrice;
	// 		$('.rp').html(rp + '.00');
	// 	}
	// });
	// //判断选择的快递方式
	// $('input[name=transporttype]').click(function(){
	// 	if($(this).attr('value')=='1'){
	// 		transportPrice = 0;
	// 		transportPrice += parseInt($(this).attr('tp'));
	// 		$('.tp').html(transportPrice + '.00');
	// 		var rp = parseInt($('.ap').text().replace(',','')) + transportPrice + payPrice;
	// 		$('.rp').html(rp + '.00');
	// 	}else if($(this).attr('value')=='2'){
	// 		transportPrice = 0;
	// 		transportPrice += parseInt($(this).attr('tp'));
	// 		$('.tp').html(transportPrice + '.00');
	// 		var rp = parseInt($('.ap').text().replace(',','')) + transportPrice + payPrice;
	// 		$('.rp').html(rp + '.00');
	// 	}
	// });
	// 
	//全局变量可供增加新收货地址和修改收货地址
	var toUrl = addNewAddress;
	var modifyAid = '';
	//添加新地址
	$("a.addresss_add").click(function(){
		$("div#all_address_bg").css("display","block");
		$("div#address_info").css("display","block");
	});
	$("div.newaddress").click(function(){
		toUrl = addNewAddress;
		$("div#all_address_bg").css("display","block");
		$("div#address_info").css("display","block");
	});

	//关闭地址信息框
	$("span.all_address_titleClose").click(function(){
		$("div#all_address_bg").css("display","none");
		$("div#address_info").css("display","none");
	});
	$("input.address_btn_quxiao").click(function(){
		$("div#all_address_bg").css("display","none");
		$("div#address_info").css("display","none");
	});

	//选择收货地址区域
	$('select[name=area1]').change(function(){
		var cid = $(":selected",this).val();
		if(cid == 'no'){
			return false;
		}
		$.post(getCityInfo , {"cid":cid},function(data){
			eval("var info=data;");
			if(info.status=='er'){
				alert('请刷新页面后重试！');
				return false;
			}
			var areaStr = '<option value="no" selected="selected">-请选择-</option>';
			for(var area in info){
				if(area!='status')
				areaStr+='<option value="'+info[area]['cid']+'">'+info[area]['city']+'</option>';
			}
			$("select[name=area2]").empty();
			$("select[name=area2]").append(areaStr);
			$("select[name=area3]").css('display','none');
		},'json');
	});
	$('select[name=area2]').change(function(){
		var cid = $(":selected",this).val();
		if(cid == 'no'){
			return false;
		}
		$.post(getCityInfo , {"cid":cid},function(data){
			eval("var info=data;");
			if(info.status=='er'){
				return false;
			}
			var areaStr = '<option value="no" selected="selected">-请选择-</option>';
			for(var area in info){
				if(area!='status')
				areaStr+='<option value="'+info[area]['cid']+'">'+info[area]['city']+'</option>';
			}
			$("select[name=area3]").empty();
			$("select[name=area3]").append(areaStr);
			$("select[name=area3]").css('display','block');
		},'json');
	});

	//提交收货地址信息
	$("input.address_btn_tijiao").click(function(){
		var address='';
		address+=$("select[name=area1] option:selected").val()+'|';
		address+=$("select[name=area2] option:selected").val()+'|';
		if($("select[name=area3]").css('display') == 'block'){
			address+= $("select[name=area3] option:selected").val();
		}
		var consignee = $("input[name=consignee]").val();
		var tel = $("input[name=tel]").val();
		var addressD = $("input[name=addressD]").val();
		var email = $("input[name=email]").val();
		// alert(toUrl);return false;
		$.post(toUrl , {'aid':modifyAid,'address':address,'consignee':consignee,'tel':tel,'addressD':addressD,'email':email},function(data){
			eval("var info=data;");
			if(info.status =='ok'){
				if(modifyAid == ''){
					var newaddress = '';
					newaddress+='<li><div class="addressname">';
					newaddress+='<input type="radio" value="" name="address" style="margin:0px 0 2px 5px;">';
					newaddress+=consignee;
		  			newaddress+='</div><div class="location">';
		  			newaddress+=info.location;
		  			newaddress+='</div><div class="moblie">';
		  			newaddress+=tel;
		  			newaddress+='</div><div class="opreate"><a class="address_defa">设为默认地址</a>';
		  			newaddress+='<a class="address_modi">[修改]</a><a class="address_del">[删除]</a></div></li>';
		  			$("ul.useraddlist").append(newaddress);
				}else if(modifyAid != ''){
					var modifyReplaceLi =  $("input[name=consignee]").parents('li');
					var newaddress = '';
					newaddress+='<li><div class="addressname">';
					newaddress+='<input type="radio" value="" name="address" style="margin:0px 0 2px 5px;">';
					newaddress+=consignee;
		  			newaddress+='</div><div class="location">';
		  			newaddress+=addressD;
		  			newaddress+='</div><div class="moblie">';
		  			newaddress+=tel;
		  			newaddress+='</div><div class="opreate"><a class="address_defa">设为默认地址</a>';
		  			newaddress+='<a class="address_modi">[修改]</a><a class="address_del">[删除]</a></div></li>';
		  			modifyReplaceLi.replaceWith(newaddress);
				}
				
	  			//关闭添加地址窗口
	  			$("div#all_address_bg").css("display","none");
				$("div#address_info").css("display","none");
			}else if(info.status =='er'){
				alert('请刷新页面后重试！');
			};
		},'json');
	});

	//删除收货地址
	$("a.address_del").live('click',function(){
		if(confirm('确定要删除该收货地址?')){
			var aid= $(this).parents('li').attr('aid');
			var that = $(this).parents('li');
			$.post(addDel , {'aid':aid},function(data){
				eval('var info=data;');
				if(info.status=='ok'){
					that.remove();
				}else if(info.status == 'er'){
					alert('请刷新页面后重试！');
				}
			},'json');
		}
	});

	//设置默认收货地址
	$('a.address_defa').live('click',function(){
		var aid= $(this).parents('li').attr('aid');
		var that=$(this);
		var parentLi = $(this).parents('li');
		var firstdefault = $('b.isdefault');
		$.post(setDefAddress ,{'aid':aid},function(data){
			eval("var info=data;")
			if(info.status=='ok'){
				that.replaceWith('<b class="isdefault">默认地址</b>');
				firstdefault.replaceWith('<a class="address_defa">设为默认地址</a>');
				parentLi.find('input[name=address]').attr('checked','checked');
			}else if(info.status=='er'){
				alert('请刷新页面后重试！');
			}
		},'json');
	});

	//修改收货地址
	$('a.address_modi').live('click',function(){
		$(this).attr('to','modifyAddress');
		var aid= $(this).parents('li').attr('aid');
		modifyAid = $(this).parents('li').attr('aid');
		$.post(modifyAddress,{'aid':aid},function(data){
			eval('var info=data;');
			var ids=info.cityid.split('|');
			var areaStr = '<option value="no" selected="selected">-请选择-</option>';
			for(var i=0;i<ids.length;i++){
				if(i ==0){
					$("select[name=area1] option").each(function(){
						if($(this).val()==ids[i]){
							$(this).attr('selected','selected');
						}
					});
				}
				if(i ==1){
					// alert(info.citys)
					for(var area in info.citys){
						if(area==0){
							for(var area1 in info.citys[area]){
								// alert(info.citys[area][area1]['city'])
								areaStr+='<option value="'+info.citys[area][area1]['cid']+'">'+info.citys[area][area1]['city']+'</option>';
							}
						}
						
					}
					$("select[name=area2]").empty().append(areaStr);
					$("select[name=area2] option").each(function(){
						if($(this).val()==ids[i]){
							$(this).attr('selected','selected');
						}
					});
				}
				if(i ==2){
					areaStr = '<option value="no" selected="selected">-请选择-</option>';
					for(var area in info.citys){
						if(area==1){
							for(var area2 in info.citys[area]){
								// alert(info.citys[area][area2]['city'])
								areaStr+='<option value="'+info.citys[area][area2]['cid']+'">'+info.citys[area][area2]['city']+'</option>';
							}
						}
						
					}
					$("select[name=area3]").css('display','block');
					$("select[name=area3]").empty().append(areaStr);
					$("select[name=area3] option").each(function(){
						if($(this).val()==ids[i]){
							$(this).attr('selected','selected');
						}
					});
				}
			}
			if(ids.length ==2){
				$("select[name=area3]").css('display','none');
			}
			// return false;
			// alert(ids.length)
			if(info.status=='ok'){
				$("div#all_address_bg").css("display","block");
				$("div#address_info").css("display","block");
				$('input[name=consignee]').val(info.consignee);
				$('input[name=tel]').val(info.moblie);
				$('input[name=email]').val(info.email);
				$('input[name=addressD]').val(info.locationinfo);
				toUrl = updateAddress;
			}else if(info.status=='er'){
				alert('请刷新页面后重试！');	
			}
		},'json')
	});



	/*======================支付方式=================*/
	//选择支付方式
	$("a.select_paytype").click(function(){
		$("div#all_address_bg").css("display","block");
		$("#paytype_list").css('display','block');
	});
	$('span.close_p_w').click(function(){
		$("#paytype_list").css('display','none');
		$("div#all_address_bg").css("display","none");
	});
	$('input.quxiao').click(function(){
		$("#paytype_list").css('display','none');
		$("div#all_address_bg").css("display","none");
	});

	//确认支付方式
	$("input.queren").click(function(){
		var payInfo = $('input[name=selectPayment]:checked').siblings('img').attr('src');
		var pstypeId = $('input[name=selectPayment]:checked').val();
		$("#paytype_list").css('display','none');
		$("div#all_address_bg").css("display","none");
		var paytypeStr = '<img src="'+payInfo+'" width="125" height="32">';
		$("a.select_paytype").html('').append(paytypeStr);
		$('input[name=paytype]').val(pstypeId);
	});

	//快递方式
	$("select[name=pstype]").change(function(){
		var trtype = $(':selected',this).val();
		var tp = 0;
		if(trtype==1){
			$("span.tp").html('15.00');
			tp =15;
		}
		if(trtype==2){
			$("span.tp").html('20.00');
			tp=20;
		}
		var ap = parseInt($('span.ap').html());
		ap = ap+tp;
		$('font.rp').html(ap+'.00');

	});

	//提交订单
	$("input.submitbtn").click(function(){
		if($('input[name=paytype]').val() == '0'){
			alert('请选择支付方式！');
			return false;
		}
	});

});
